import Vue from 'vue'
import Vuex from 'vuex'

import {
	apiBaseUrl
} from '../config/api.js'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		userInfo: {},
		footPrint: [], //商品浏览历史
		token: '',
		currentCity: '', //当前选择城市
		applicationConfig: {}, //应用全局设置
		apiBaseUrl: apiBaseUrl,
		openid: null,
		session_key: "",
	},
	mutations: {
		setOpenId(state, res) {
			state.openid = res.openid
			state.session_key = res.session_key
		},
		addFootPrint(state, provider) {
			var oldFootPrint = state.footPrint;
			var footPrint = [];
			for (var i = 0; i < oldFootPrint.length; i++) {
				if (oldFootPrint[i].productUuid != provider.productUuid) {
					footPrint.push(oldFootPrint[i]);
				}
			}
			footPrint.push(provider);
			//只保留20个商品浏览历史
			if (footPrint.length > 20) {
				footPrint = footPrint.slice(-20);
			}
			state.footPrint = footPrint;
			uni.setStorage({ //缓存应用全局设置
				key: 'footPrint',
				data: footPrint
			})
		},
		updateApplicationConfig(state, provider) {
			state.applicationConfig = provider;
			uni.setStorage({ //缓存应用全局设置
				key: 'applicationConfig',
				data: provider
			})
		},
		changeCity(state, provider) {
			state.currentCity = provider;

			uni.setStorage({ //缓存当前城市设置
				key: 'currentCity',
				data: provider
			})
		},
		login(state, provider) {
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorage({ //缓存用户登陆状态
				key: 'userInfo',
				data: provider
			})
			// console.log(state.userInfo);
		},
		setToken(state, token) {
			// console.log(token);
			state.token = token
			uni.setStorageSync('userToken', token)
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};

			uni.removeStorage({
				key: 'userInfo'
			})
			uni.removeStorage({
				key: 'userToken'
			})
		}
	},
	actions: {

	}
})

export default store
